package com.wdb007.venus.svc.biz.manager;

import java.net.URI;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("sslClientApp")
public class SSLClientApp{
	
	private static Logger logger = LoggerFactory.getLogger(SSLClientApp.class);
	
	final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {  
        public boolean verify(String hostname, SSLSession session) {  
            return true;  
        }  
    };  
  
  
    /** 
     * Trust every server - dont check for any certificate 
     */  
    public void trustAllHosts(WebSocketChatClient appClient) {  
        // Create a trust manager that does not validate certificate chains  
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {  
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
                return new java.security.cert.X509Certificate[]{};  
            }  

			@Override
			public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
					throws java.security.cert.CertificateException {
				
			}
			@Override
			public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
					throws java.security.cert.CertificateException {
			}  
        }};  
  
  
        // Install the all-trusting trust manager  
        try {  
            SSLContext sc = SSLContext.getInstance("TLS");  
            sc.init(null, trustAllCerts, new java.security.SecureRandom());  
            appClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    public SSLClientApp() {}
    
    private WebSocketChatClient client = null ;
    
    public void setURI(URI uri) {
    	this.client = new WebSocketChatClient(uri);
    	try {
    		if("wss".equals(uri.getScheme())) {
    			trustAllHosts(client);
    		}
			this.client.connect();
		} catch (Exception e) {
			logger.error("连接推送服务器异常", e);
		}
    }
    
    public void reset(URI uri) {
    	this.client = new WebSocketChatClient(uri);
    	try {
    		if("wss".equals(uri.getScheme()))
    			trustAllHosts(client);
			this.client.connect();
		} catch (Exception e) {
			logger.error("连接推送服务器异常", e);
		}
    }
    
    public void send(String data) {
    	this.client.send(data);
    }
    
    
    public void send(URI uri, String data) {
    	WebSocketChatClient chatclient = new WebSocketChatClient(uri);
    	if("wss".equals(uri.getScheme())) {
    		trustAllHosts(chatclient );
    	}
    	chatclient.connect();
    } 
    
    
    
    public static void main( String[] args ) throws Exception {  
    	SSLClientApp client = new SSLClientApp();
    	client.send(new URI( "wss://ws.wdb007.com:8090/?action=bookshelf&bookshelfcode=0021200060&token=wwwww" ), "");
  
    }
}
